<template>
	<view class="container">
		<view class="content">
			<view class="remark">
				<text>共 <text class="remark-num">{{cartList.length}}</text> 件商品</text>
				<text class="remark-edit" @click="edit()">编辑</text>
			</view>
			<!-- 空状态 -->
			<view class="empty" v-if="cartList.length == 0">
				<text class="empty-title">购物车空空如也</text>
				<text class="empty-content">赶紧慰劳一下自己吧</text>
				<button @click="toHome">去逛逛</button>
			</view>

			<!-- 购物车商品 -->
			<view class="cart" v-else>
				<view class="cart-goods">
					<checkbox-group @change="checkboxChange">
						<view
                class="cart-item" v-for="(item, index) in cartList" :key="item.id">
							<view>
								<label>
									<checkbox class="round red" :checked="item.checked" :id="item.id" :value="item.id" color="#f56c6c"
										active-border-color="#f56c6c" active-background-color="#f56c6c"
										icon-color="#FFFFFF" style="transform: scale(0.8)" />
								</label>
							</view>
							<view class="goods" @click.stop="toDetail(item.product.id)">
								<view class="goods-image">
									<image :src="item.product.post_url"></image>
								</view>
								<view class="goods-right">
									<view class="goods-name">
										{{item.product.title}}
									</view>
									<view class="goods-tag">
										<text v-for="(i, index) in (item.tags ? item.tas.split(',') : [])" :key="index">
											{{i}}
										</text>
									</view>
									<view class="goods-old-price" v-if="item.product.old_price"><text>￥{{item.product.old_price}}</text></text></view>
									<view class="goods-price">
										<view>
											￥<text class="goods-price-txt">{{item.product.price}}</text>
										</view>
										<view>
											<u-number-box
                          @change="updateCartNum(item)"
                          v-model="item.count" bg-color="#FFFFFF" :min="1" :max="99"
												:step="1" :index="item.id"></u-number-box>
										</view>
									</view>
								</view>
							</view>
						</view>
					</checkbox-group>
				</view>
			</view>

			<!-- 为你推荐 -->
			<view class="recommend">
				<view class="recommend-title">
					<image class="recommend-title-l" src="/static/icon/tit-icon-l.png"></image>
					<text>为你推荐</text>
					<image class="recommend-title-r" src="/static/icon/tit-icon-r.png"></image>
				</view>
				<view class="recommend-goods">
					<view class="recommend-item" v-for="(item, index) in recommendList" :key="item.id">
						<image class="recommend-item-image" :src="item.post_url"></image>
						<view class="recommend-item-name">
							{{item.title}}
						</view>
						<view class="recommend-item-tag">
							<text v-for="(i, index) in (item.tags ? item.tas.split(',') : [])" :key="index">
								{{i}}
							</text>
						</view>
						<view class="recommend-item-price">
							<text class="recommend-item-price-txt">￥{{item.price}}<text
									class="recommend-item-price-unit">
									/{{item.unit}}</text></text>
							<image class="recommend-item-shopcar-icon" src="/static/icon/shopcar.svg"></image>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部栏 -->
		<view class="bottombar">
			<view class="bottombar-content">
				<view class="bottombar-left">
					<checkbox class="round red" value="全选" :checked="checkedAll" color="#f56c6c"
						active-border-color="#f56c6c" active-background-color="#f56c6c" icon-color="#FFFFFF"
						style="transform: scale(0.8);" @click="selectAll()" />
					<text>全选</text>
				</view>
				<view class="bottombar-right" v-if="!editState">
					<text>合计：</text>
					<text class="bottombar-price">￥{{totalPrice}}</text>
					<button @click="goPayment()">去结算</button>
				</view>
				<view class="bottombar-right" v-else>
					<button class="del-btn" @click="delGoods()">删除</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cartList: [
				],
				recommendList: [
				],
				checkedAll: false,
				num: 1,
				price: 0,
				editState: false
			};
		},
		computed: {
			// 计算总价
			totalPrice() {
				let tprice = 0;
				var items = this.cartList;
				for (var i = 0, len = items.length; i < len; ++i) {
					const item = items[i];
					const price = this.$utils.mathMul(item.product.price, item.count);
					if (item.checked) {
						tprice += price;
					}
				}
				return this.$utils.numberFormat(tprice);
			}
		},
    onShow() {
      if (this.$store.state.userInfo.id) {
        this.getCartList()
      }

      this.$api.getProductList({
        is_recommend: 1,
        pageSize: 20
      }).then(res => {
        this.recommendList = res.rows
      })
    },
		methods: {
      toDetail(id) {
        uni.navigateTo({
          url: '/pages/goods/goods?id=' + id
        })
      },
      toHome() {
        uni.switchTab({
          url: '/pages/index/index'
        })
      },
      getCartList() {
        this.$api.getCartList({
          user_id: this.$store.state.userInfo.id,
          pageSize: 99999
        }).then(res => {
          this.cartList = res.rows.map(item => {
            return {
              ...item,
              product: item.product || {},
              checked: item.status === 1
            }
          })
          this.checkedAll = this.cartList.every(item => item.checked)
          if (this.cartList.length === 0) {
            this.checkedAll = false
          }
        })
      },
      updateCartNum(item) {
        this.$api.updateCart({
          id: item.id,
          count: item.count
        })
      },
			checkboxChange: function(e) {
        console.log(e)
				let price = 0;
				let items = this.cartList,
					values = e.detail.value.map(item => Number(item));
        let allState = true;

				// 对有复选框的数组集合进行遍历
				for (let i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i];
					// 判断当前商品是否在选中的ID列表中
					if (values.includes(item.id)) {
            item.checked = true
					} else {
            item.checked = false
						allState = false; // 如果有商品未选中，取消全选状态
					}
					if (!item.checked) {
						allState = false;
					}
				}
				this.checkedAll = allState;
        // 更新所有选中状态有
        this.$api.updateCartStatusAll({
          user_id: this.$store.state.userInfo.id,
          checkedId: this.cartList.filter(item => item.checked).map(item => item.id)
        })
			},
			// 选中/取消选中全部
			selectAll() {
        if (!this.cartList.length) {
          return
        }
				let price = 0;
				var items = this.cartList;
				// 根据 checkedAll 的状态，决定是选中所有还是取消所有
				const newCheckedState = !this.checkedAll;
				for (var i = 0, len = items.length; i < len; ++i) {
					this.$set(items[i], 'checked', newCheckedState);
				}
				// 最后更新 checkedAll 的状态
				this.checkedAll = newCheckedState;
        // 更新所有选中状态有
        this.$api.updateCartStatusAll({
          user_id: this.$store.state.userInfo.id,
          checkedId: this.cartList.filter(item => item.checked).map(item => item.id)
        })
			},
			// 跳转结算页面
			goPayment() {
				if (this.totalPrice == 0.00) {
					this.$utils.toastTip("请先选择商品");
					return;
				}
				uni.navigateTo({
					url: "/pages/cart/payment"
				})
			},
			// 编辑商品
			edit() {
				this.editState = !this.editState;
			},
			// 删除商品
			delGoods() {
				let arr = this.cartList;
        let newArr = arr.filter(item => item.checked);
				let that = this;
				this.$utils.showConfirm("确定要删除选中的商品吗？").then(res => {
					if (res.confirm) {
            this.$api.deleteCart({
              ids: newArr.map(item => item.id)
            }).then(res => {
              this.getCartList()
              that.$utils.toastTip("删除成功！");
            })
					}
				});
			}

		}
	}
</script>

<style lang="scss">
	page {
		background: #F8F9FA;
		font-family: PingFangSC;
	}

	/*每个页面公共css */
	/* //设置圆角 */
	checkbox.round .wx-checkbox-input,
	checkbox.round .uni-checkbox-input {
		border-radius: 100upx;
		border: 2px solid #f56c6c;
	}

	/* //设置背景色 */
	checkbox.red[checked] .wx-checkbox-input,
	checkbox.red.checked .uni-checkbox-input {
		background-color: #f56c6c !important;
		border-color: #f56c6c !important;
		color: #FFFFFF !important;
	}

	uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked:before {
		content: '\2022';
	}

	.u-numberbox {

		.u-icon-minus,
		.u-icon-plus {
			width: 44rpx;
			height: 44rpx;
			background: #F8F9FA !important;
			border-radius: 50%;

			.u-icon {
				color: #333333;
			}
		}

		.u-icon-disabled {
			background: #F8F9FA !important;
		}
	}
</style>

<style lang="scss" scoped>
	.content {
		padding: 20rpx 30rpx;
	}

	.remark {
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx 20rpx;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.remark-num {
			font-weight: 600;
		}

		.remark-edit {
			color: #f56c6c;
		}
	}

	.empty {
		display: flex;
		flex-direction: column;
		align-items: center;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 60rpx 30rpx;

		.empty-title {
			font-size: 32rpx;
			color: #333333;
		}

		.empty-content {
			font-size: 28rpx;
			color: #999999;
			margin-top: 10rpx;
		}

		button {
			border-radius: 36rpx;
			background-color: #FFFFFF;
			margin: 0;
			font-size: 24rpx;
			color: #f56c6c;
			border: 1rpx solid #f56c6c;
			margin-top: 30rpx;
		}

		button::after {
			border: none;
		}
	}

	.cart {
		display: flex;
		flex-direction: column;
		align-items: center;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx 20rpx;

		.cart-goods {
			width: 100%;
		}
	}

	.recommend {
		margin-top: 50rpx;
		margin-bottom: 100rpx;

		.recommend-title {
			display: flex;
			align-items: center;
			justify-content: center;

			text {
				font-size: 30rpx;
				font-weight: 600;
				color: #333333;
			}

			image {
				width: 40rpx;
				height: 22rpx;
			}

			.recommend-title-l {
				margin-right: 30rpx;
			}

			.recommend-title-r {
				margin-left: 30rpx;
			}
		}

		.recommend-goods {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.recommend-item {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				border-radius: 20rpx;
				background: #FFFFFF;
				padding: 0rpx 30rpx 30rpx 30rpx;
				margin-top: 30rpx;
			}

			.recommend-item-image {
				width: 280rpx;
				height: 280rpx;
				border-radius: 4px;
			}

			.recommend-item-name {
				width: 274rpx;
				font-size: 26rpx;
				font-weight: 700;
				line-height: 34rpx;
				color: #333333;
				margin-top: 10rpx;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}

			.recommend-item-tag {
				display: flex;
				align-items: center;
				margin-top: 14rpx;
				font-size: 18rpx;

				text:nth-child(1) {
					color: #F55726;
					border: 1px solid #F55726;
					border-radius: 4rpx;
					padding: 0rpx 4rpx;
				}

				text:nth-child(2) {
					color: #f56c6c;
					border: 1px solid #f56c6c;
					border-radius: 4rpx;
					margin-left: 10rpx;
					padding: 0rpx 4rpx;
				}
			}

			.recommend-item-price {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 14rpx;

				.recommend-item-price-txt {
					color: #F55726;
					font-size: 32rpx;
					font-weight: 600;

					.recommend-item-price-unit {
						font-size: 22rpx;
						color: #999999;
						font-weight: normal;
					}
				}

				.recommend-item-shopcar-icon {
					width: 44rpx;
					height: 44rpx;
					margin-right: 10rpx;
				}
			}
		}
	}

	.cart-item {
		display: flex;
		align-items: center;
		margin-bottom: 40rpx;
	}

	.goods {
		display: flex;
		align-items: center;
		padding: 0rpx 16rpx;
		width: 100%;

		.goods-right {
			margin-left: 24rpx;
			width: 100%;
		}

		.goods-image {
			image {
				width: 180rpx;
				height: 180rpx;
				border-radius: 4px;
			}
		}

		.goods-name {
			font-size: 26rpx;
			font-weight: 700;
			line-height: 34rpx;
			color: #333333;
			margin-top: 10rpx;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
		}

		.goods-tag {
			display: flex;
			align-items: center;
			margin-top: 14rpx;
			font-size: 18rpx;

			text:nth-child(1) {
				color: #F55726;
				border: 1px solid #F55726;
				border-radius: 4rpx;
				padding: 0rpx 4rpx;
			}

			text:nth-child(2) {
				color: #f56c6c;
				border: 1px solid #f56c6c;
				border-radius: 4rpx;
				margin-left: 10rpx;
				padding: 0rpx 4rpx;
			}
		}

		.goods-old-price {
			font-size: 22rpx;
			color: #CCCCCC;
			text-decoration: line-through;
			margin: 20rpx 0rpx 4rpx 0rpx;
		}

		.goods-price {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			color: #F55726;
			font-size: 24rpx;

			.goods-price-txt {
				font-size: 32rpx;
				font-weight: 600;

				.goods-price-unit {
					font-size: 22rpx;
					color: #999999;
					font-weight: normal;
				}
			}
		}
	}

	.bottombar {
		position: fixed;
		bottom: var(--window-bottom, 0);
		width: 100%;
		background: #FFFFFF;

		.bottombar-content {
			padding: 16rpx 30rpx 20rpx 16rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.bottombar-left {

			text {
				font-size: 24rpx;
				color: #333333;
			}
		}

		.bottombar-right {
			display: flex;
			align-items: center;


			button {
				border-radius: 36rpx;
				background: #f56c6c;
				color: #FFFFFF;
				font-size: 24rpx;
			}

			.del-btn {
				background: #F55726;
			}


			text {
				color: #333333;
				font-size: 24rpx;
			}

			.bottombar-price {
				font-size: 32rpx;
				font-weight: 600;
				margin-right: 30rpx;
			}
		}
	}
</style>
